package com.tommy.ddp.manager
{
	import com.tommy.ddp.GridDataManager;
	import com.tommy.ddp.MainManager;
	import com.tommy.ddp.box.DdpBox;
	import com.tommy.ddp.definition.DDPConfig;
	import com.tommy.ddp.events.FinderEvent;
	import com.tommy.ddp.grid.DdpGrid;
	import com.tommy.ddp.swapHandle.AdvSwapHandler;
	
	import flash.utils.setTimeout;

	/**
	 * 消除后填充空格的管理
	 * @author hongxuan
	 * 
	 */	
	public class FillUpManager
	{
		private static var movedBoxNum:uint;
		private static var finishMoveBoxList:Vector.<DdpBox>;
		private static var swaper:AdvSwapHandler;
		
		public function FillUpManager()
		{
		}
		
		/*public static function fillUp():void
		{
			return;
			trace("填充");
			//MainManager.game.container.mouseEnabled = false;
			//
			movedBoxNum = 0;
			finishMoveBoxList = new Vector.<DdpBox>();
			
			for(var i:int = 0;i<DDPConfig.W_NUM;i++)
			{
				computer(i);
			}
		}*/
		/**
		 * 计算每一列需要补充多少个
		 * @param v 列
		 * 
		 */		
		private static function computer(v:int):void
		{
			var avaliableGridNum:uint;
			var avaliableGridIndexList:Vector.<int> = new Vector.<int>();
			var boxNum:uint;
			//当前列所有的BOX集合
			var allColumnBoxList:Vector.<DdpBox> = new Vector.<DdpBox>();
			
			for(var i:uint = 0;i<DDPConfig.H_NUM;i++)
			{
				var grid:DdpGrid = GridDataManager.getGrid(v,i);
				if(grid.isAvaliable)
				{
					avaliableGridNum++;
					avaliableGridIndexList.push(i);
				}
			}
			
			for(i = 0;i<DDPConfig.H_NUM;i++)
			{
				var box:DdpBox = GridDataManager.getBox(v,i);
				if(box)
				{
					allColumnBoxList.push(box);
					boxNum++;
				}
			}
			
			var missingNum:int = avaliableGridNum - boxNum;
			for(i = 0;i<missingNum;i++)
			{
				var newBox:DdpBox = new DdpBox();
				newBox.column = v;
				newBox.row = -i-1;
				newBox.setType(Math.floor(Math.random()*DDPConfig.COLOR_NUM));
				MainManager.game.container.addChild(newBox);
				
				allColumnBoxList.unshift(newBox);
			}
			setTimeout(function():void
			{
			//|||settime
			var count:uint = 0;
			for each(var j:DdpBox in allColumnBoxList)
			{
				var b:Boolean = j.moveRow(avaliableGridIndexList[count]);
				if(b)
					movedBoxNum++;
				//s
				count++;
			}},1000);
		}
		
		public static function addFinishNum(box:DdpBox):void
		{
			finishMoveBoxList.push(box);
			if(finishMoveBoxList.length >= movedBoxNum)
			{
				trace("掉落完成，检查");
				if(!swaper)
				{
					swaper = new AdvSwapHandler();
					swaper.addEventListener(FinderEvent.NONE_TO_DISPOSE,onNoneToDispose);
					/*swaper.dispose();
					swaper.removeEventListener(FinderEvent.NONE_TO_DISPOSE,onNoneToDispose);*/
				}
				swaper.execute();
			}
		}
		
		private static function onNoneToDispose(event:FinderEvent):void
		{
			trace("检查完毕，无可消除内容，检查是否有小炸弹");
			if(DisposeBoxManager.boomNum == 0)
			{
				trace("无小炸弹");
				MainManager.game.container.mouseEnabled = true;
			}
			else
			{
				MainManager.game.tips = "3秒后清除小炸弹……";
				trace("3秒后清除小炸弹……");
				setTimeout(function():void
				{
					DisposeBoxManager.clearBoom();
				},3000);
			}
		}
		
		public static function checkAgain():void
		{
			if(!swaper)
			{
				swaper = new AdvSwapHandler();
				swaper.addEventListener(FinderEvent.NONE_TO_DISPOSE,onNoneToDispose);
			}
			swaper.execute();
		}
	}
}